package com.yyc.stocktake.ui.frg

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.yyc.stocktake.R
import com.yyc.stocktake.adapter.OrderAdapter
import com.yyc.stocktake.ext.init
import com.yyc.stocktake.ext.loadServiceInit
import com.yyc.stocktake.ext.showLoading
import com.yyc.stocktake.viewmodel.OrderModel
import com.yyc.stocktake.weight.recyclerview.SpaceItemDecoration
import com.kingja.loadsir.core.LoadService
import com.yyc.stocktake.MainActivity
import com.yyc.stocktake.api.UIHelper
import com.yyc.stocktake.base.BaseFragment
import com.yyc.stocktake.bean.db.OrderBean
import com.yyc.stocktake.databinding.FOrderBinding
import com.yyc.stocktake.ext.ORDER_TYPE
import com.yyc.stocktake.ext.setNbOnItemClickListener
import com.yyc.stocktake.mar.eventViewModel
import com.yyc.stocktake.viewmodel.RfidModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @Author nike
 * @Date 2023/7/7 11:59
 * @Description
 */
class OrderFrg : BaseFragment<OrderModel, FOrderBinding>(){

    val adapter: OrderAdapter by lazy { OrderAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    private val rfidModel: RfidModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {

        }

        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(10f), ConvertUtils.dp2px(10f), true))
        }

        val navController = NavHostFragment.findNavController(this)
        navController.popBackStack(R.id.action_nav_order_to_assetFrg, true)
        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->
                val bean = adapter.data[position] as OrderBean
                UIHelper.startAssetFrg(nav(), bean.stocktakeno)
            }
        }

        mDatabind.swipeRefresh.isEnabled = false
        mDatabind.includeSearch.etText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                adapter!!.filter.filter(p0.toString())
            }

            override fun afterTextChanged(p0: Editable?) {
            }
        })

        mDatabind.includeSearch.ivQr.setOnClickListener {
            UIHelper.startZxingAct(ORDER_TYPE)
        }

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                (activity as? MainActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(false); // 显示返回箭头
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                rfidModel.scanData.value = null
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            loadsir.showSuccess()
            adapter.setList(it)
            adapter!!.appendList(it)
        })
        eventViewModel.mainListEvent.observeInFragment(this, Observer {
            mViewModel.onRequest()
        })
        eventViewModel.zkingType.observeInFragment(this, Observer {
            if (it.type == ORDER_TYPE){
                rfidModel.scanData.value = it.text
            }
        })
        rfidModel.scanData.observe(viewLifecycleOwner, { scanData ->
            if (scanData != null && viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                val bean = adapter.data.firstOrNull { it.stocktakeno == scanData }
                bean?.let { UIHelper.startAssetFrg(nav(), it.stocktakeno) }
            }
        })
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        //设置界面 加载中
        loadsir.showLoading()
        mViewModel.onRequest()
    }

}